package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Author bo.li
 * @Date 2023/3/16 11:08
 * @Version 1.0
 */
@RestController
@Slf4j
public class WmNewsController {

    @Autowired
    private WmNewsService wmNewsService;
    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    /**
     * 文章分页:是否收藏
     * @param reqDto
     * @return
     */
    @PostMapping("/api/v1/news/list")
    public ResponseResult list(@RequestBody WmNewsPageReqDto reqDto){
        return wmNewsService.pageList(reqDto);
    }

    /**
     * 新增文章
     * @param newsDto
     * @return
     */
    @PostMapping("/api/v1/news/submit")
    public ResponseResult submit(@RequestBody WmNewsDto newsDto){
        return wmNewsService.submit(newsDto);
    }


    /**
     * 分页查询根据状态和标题
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/news/list_vo")
    public PageResponseResult listByStatusAndTitle(@RequestBody NewsAuthDto dto){
        PageResponseResult result = wmNewsService.listByStatusAndTitle(dto);
        return result;
    }

    /**
     * 根据id查询新闻
     * @return
     */
    @GetMapping("/api/v1/news/one_vo/{id}")
    public ResponseResult quireById(@PathVariable Integer id){
        log.info("根据id查询文章:{}",id);
        WmNews wmNews = wmNewsService.getById(id);
        return ResponseResult.okResult(wmNews);
    }

    /**
     * 查询文章详情,
     * @param id
     * @return
     */
    @GetMapping("/api/v1/news/one/{id}")
    public ResponseResult quireOneById(@PathVariable Integer id){
        log.info("根据id查询文章:{}",id);
        WmNews wmNews = wmNewsService.getById(id);
        return ResponseResult.okResult(wmNews);
    }

    /**
     * 人工审核:审核不通过
     * @return
     */
    @PostMapping("/api/v1/news/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDto dto){
        log.info("审核通过");
        wmNewsService.authFail(dto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 人工审核:审核通过
     * @return
     */
    @PostMapping("/api/v1/news/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDto dto){
        log.info("审核不通过");
        wmNewsService.authPass(dto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @GetMapping("/api/v1/news/del_news/{id}")
    public ResponseResult deleteById(@PathVariable Integer id){
        log.info("根据id删除文章:{}",id);
        wmNewsService.removeById(id);
        LambdaQueryWrapper<WmNewsMaterial> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(WmNewsMaterial::getNewsId,id);
        wmNewsMaterialMapper.delete(wrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}